package com.agile.major.web.rest;

import com.agile.major.domain.ProductionAndLearningCooperation;
import com.agile.major.service.ProductionAndLearningCooperationService;
import com.agile.major.web.rest.util.HeaderUtil;
import com.agile.major.web.rest.util.PaginationUtil;
import com.codahale.metrics.annotation.Timed;
import io.github.jhipster.web.util.ResponseUtil;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Optional;

/**
 * REST controller for managing ProductionAndLearningCooperation.
 */
@RestController
@RequestMapping("/api")
public class ProductionAndLearningCooperationResource {

    private final Logger log = LoggerFactory.getLogger(ProductionAndLearningCooperationResource.class);

    private static final String ENTITY_NAME = "productionAndLearningCooperation";

    private final ProductionAndLearningCooperationService productionAndLearningCooperationService;

    public ProductionAndLearningCooperationResource(ProductionAndLearningCooperationService productionAndLearningCooperationService) {
        this.productionAndLearningCooperationService = productionAndLearningCooperationService;
    }

    /**
     * POST  /production-and-learning-cooperations : Create a new productionAndLearningCooperation.
     *
     * @param productionAndLearningCooperation the productionAndLearningCooperation to create
     * @return the ResponseEntity with status 201 (Created) and with body the new productionAndLearningCooperation, or with status 400 (Bad Request) if the productionAndLearningCooperation has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/production-and-learning-cooperations")
    @Timed
    public ResponseEntity<ProductionAndLearningCooperation> createProductionAndLearningCooperation(@RequestBody ProductionAndLearningCooperation productionAndLearningCooperation) throws URISyntaxException {
        log.debug("REST request to save ProductionAndLearningCooperation : {}", productionAndLearningCooperation);
        if (productionAndLearningCooperation.getId() != null) {
            return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert(ENTITY_NAME, "idexists", "A new productionAndLearningCooperation cannot already have an ID")).body(null);
        }
        ProductionAndLearningCooperation result = productionAndLearningCooperationService.save(productionAndLearningCooperation);
        return ResponseEntity.created(new URI("/api/production-and-learning-cooperations/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /production-and-learning-cooperations : Updates an existing productionAndLearningCooperation.
     *
     * @param productionAndLearningCooperation the productionAndLearningCooperation to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated productionAndLearningCooperation,
     * or with status 400 (Bad Request) if the productionAndLearningCooperation is not valid,
     * or with status 500 (Internal Server Error) if the productionAndLearningCooperation couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/production-and-learning-cooperations")
    @Timed
    public ResponseEntity<ProductionAndLearningCooperation> updateProductionAndLearningCooperation(@RequestBody ProductionAndLearningCooperation productionAndLearningCooperation) throws URISyntaxException {
        log.debug("REST request to update ProductionAndLearningCooperation : {}", productionAndLearningCooperation);
        if (productionAndLearningCooperation.getId() == null) {
            return createProductionAndLearningCooperation(productionAndLearningCooperation);
        }
        ProductionAndLearningCooperation result = productionAndLearningCooperationService.save(productionAndLearningCooperation);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, productionAndLearningCooperation.getId().toString()))
            .body(result);
    }

    /**
     * GET  /production-and-learning-cooperations : get all the productionAndLearningCooperations.
     *
     * @param pageable the pagination information
     * @return the ResponseEntity with status 200 (OK) and the list of productionAndLearningCooperations in body
     */
    @GetMapping("/production-and-learning-cooperations")
    @Timed
    public ResponseEntity<List<ProductionAndLearningCooperation>> getAllProductionAndLearningCooperations(@ApiParam Pageable pageable) {
        log.debug("REST request to get a page of ProductionAndLearningCooperations");
        Page<ProductionAndLearningCooperation> page = productionAndLearningCooperationService.findAll(pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/production-and-learning-cooperations");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

    /**
     * GET  /production-and-learning-cooperations/:id : get the "id" productionAndLearningCooperation.
     *
     * @param id the id of the productionAndLearningCooperation to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the productionAndLearningCooperation, or with status 404 (Not Found)
     */
    @GetMapping("/production-and-learning-cooperations/{id}")
    @Timed
    public ResponseEntity<ProductionAndLearningCooperation> getProductionAndLearningCooperation(@PathVariable Long id) {
        log.debug("REST request to get ProductionAndLearningCooperation : {}", id);
        ProductionAndLearningCooperation productionAndLearningCooperation = productionAndLearningCooperationService.findOne(id);
        return ResponseUtil.wrapOrNotFound(Optional.ofNullable(productionAndLearningCooperation));
    }

    /**
     * DELETE  /production-and-learning-cooperations/:id : delete the "id" productionAndLearningCooperation.
     *
     * @param id the id of the productionAndLearningCooperation to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/production-and-learning-cooperations/{id}")
    @Timed
    public ResponseEntity<Void> deleteProductionAndLearningCooperation(@PathVariable Long id) {
        log.debug("REST request to delete ProductionAndLearningCooperation : {}", id);
        productionAndLearningCooperationService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }

    @GetMapping("/production-and-learning-cooperations/{id}/{year}")
    @Timed
    public ResponseEntity<List<ProductionAndLearningCooperation>> getProductionCooperationMap(@PathVariable Long id,@PathVariable String year) {
        List<ProductionAndLearningCooperation> productionAndLearningCooperation = productionAndLearningCooperationService.getProductionCooperationByMajorAndYear(id,year);
        return new ResponseEntity<>(productionAndLearningCooperation, HttpStatus.OK);

    }
}
